package com.pengpaishuzi.service.impl;

import com.alibaba.fastjson2.JSON;
import com.pengpaishuzi.dao.OrderDao;
import com.pengpaishuzi.domain.Order;
import com.pengpaishuzi.domain.Product;
import com.pengpaishuzi.feign.ProductFeignApi;
import com.pengpaishuzi.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private ProductFeignApi productFeignApi;

    @Override
    public Order createOrder(Long productId, Long userId) {

        log.info("接受到:{}号商品的下单请求，查询调用商品微服务查询商品信息", productId);

        Product product = productFeignApi.findeByPid(productId);
        log.info("查询到{}号商品信息，内容是：{}", productId, JSON.toJSONString(product));

        Order order = new Order();
        order.setUid(userId);
        order.setUsername("lijunjie");
        order.setPid(productId);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);

        orderDao.save(order);

        return order;
    }
}
